import { dayjs } from 'element-plus';
import {
  DATE_CUTS,
  FORMAT_DATE,
  INIT_SEARCH_TIME,
  GET_USER_IDS
} from '@/utils/const';
import userStore from '@/store/modules/user';

// 注入组件内数据
const componentData = ref({});
export const injectData = obj => {
  componentData.value = { ...componentData.value, ...obj };
};

// 查询表单
export const searchParams = reactive({
  applyRange: DATE_CUTS.PREV_MONTH()
});

watch(
  () => searchParams.applyRange,
  val => {
    INIT_SEARCH_TIME(searchParams, val, 'begin_at', 'end_at');
  },
  { immediate: true }
);

// 查询条件
const startApplyTime = ref(undefined);
export const formItems = reactive({
  advertisement_account_id: {
    component: 'input',
    label: '广告账户ID',
    placeholder: '请输入广告账户ID',
    maxlength: 100
  },
  advertisement_account: {
    component: 'input',
    label: '广告账户名称',
    placeholder: '请输入广告账户名称',
    maxlength: 100
  },
  user_name: {
    component: 'input',
    label: '使用人员',
    showOnly: true,
    placeholder: '请输入使用人员',
    maxlength: 100,
    input() {
      searchParams.user_ids = GET_USER_IDS(searchParams.user_name);
    }
  },
  advertisement_account_type_id: {
    component: 'select',
    filterable: true,
    label: '账户类型',
    clearable: false,
    showOnly: true,
    options: [],
    change(val) {
      if (val) searchParams.advertisement_account_type_ids = [val];
      else delete searchParams.advertisement_account_type_ids;
    }
  },
  business_group_id: {
    component: 'select',
    filterable: true,
    label: '业务组',
    clearable: false,
    showOnly: true,
    options: [],
    change(val) {
      if (val) searchParams.business_group_ids = [val];
      else delete searchParams.business_group_ids;
    }
  },
  applyRange: {
    component: 'date-picker',
    type: 'daterange',
    label: '使用时间',
    clearable: false,
    showOnly: true,
    shortcuts: [
      { text: '最近7天', value: () => DATE_CUTS.LAST_7_DAYS() },
      { text: '本周', value: () => DATE_CUTS.THIS_WEEK() },
      { text: '本月', value: () => DATE_CUTS.THIS_MONTH() },
      { text: '上周', value: () => DATE_CUTS.PREV_WEEK() },
      { text: '上月', value: () => DATE_CUTS.PREV_MONTH() },
      { text: '最近31天', value: () => DATE_CUTS.LAST_31_DAYS() },
      { text: '最近180天', value: () => DATE_CUTS.LAST_180_DAYS() }
    ],
    disabledDate(ev) {
      if (!startApplyTime.value) return false;
      return (
        dayjs(ev).isAfter(dayjs(startApplyTime.value).add(179, 'day')) ||
        dayjs(ev).isBefore(dayjs(startApplyTime.value).subtract(179, 'day'))
      );
    },
    calendarChange(ev) {
      if (ev?.length && ev[0] && !ev[1]) startApplyTime.value = ev[0];
      else startApplyTime.value = undefined;
    }
  }
});

// 表格字段
export const columns = ref([
  { prop: 'advertisement_account_id', label: '广告账户ID', fixed: 'left' },
  { prop: 'advertisement_account', label: '广告账户名称' },
  { prop: 'group.name', label: '所属团队' },
  {
    prop: 'user_id',
    label: '使用人员',
    format(data) {
      return userStore().userMap[data]?.label;
    }
  },
  { prop: 'advertisement_account_type.name', label: '账户类型' },
  { prop: 'business_group.name', label: '业务组' },
  {
    prop: 'start_time',
    label: '使用开始时间',
    minWidth: 170,
    format(data) {
      return FORMAT_DATE(data, 'YYYY-MM-DD HH:mm:ss');
    }
  },
  {
    prop: 'end_time',
    label: '使用结束时间',
    minWidth: 170,
    format(data) {
      return FORMAT_DATE(data, 'YYYY-MM-DD HH:mm:ss');
    }
  }
]);
